package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserOrderAccumulatesRequest;
import com.frogs.ucenter.v1.request.UserOrderAddRequest;
import com.frogs.ucenter.v1.request.UserOrderBackRequest;
import com.frogs.ucenter.v1.request.UserOrderDetailRequest;
import com.frogs.ucenter.v1.request.UserOrderListRequest;
import com.frogs.ucenter.v1.request.UserOrderLogAddRequest;
import com.frogs.ucenter.v1.request.UserOrderLogDelRequest;
import com.frogs.ucenter.v1.service.UserOrderService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userOrderController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserOrderController extends BaseController3 {
	
	private static final String ROOT = "/user/order";
	private static final String USER_ORDER_ACCUMULATES = ROOT + "/accumulates";
	private static final String USER_ORDER_BACK = ROOT + "/back";
	
	private static final String USER_ORDER_LIST = ROOT + "/list";
	private static final String USER_ORDER_DETAIL = ROOT + "/detail";
	private static final String USER_ORDER_ADD = ROOT + "/add";
	
	private static final String USER_ORDER_SYNC = ROOT + "/sync";
	private static final String USER_ORDER_UPDATE = ROOT + "/update";
	private static final String USER_ORDER_DELETE = ROOT + "/delete";
	
	
	@Resource(name=ApiConstants.VERSION+"userOrderService")
	private UserOrderService userOrderService;
	
	@RequestMapping(USER_ORDER_SYNC)
	public Response orderSync(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderLogAddRequest req = new UserOrderLogAddRequest();
			req.parse(request);
			response = userOrderService.insertOrderLog(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_ORDER_UPDATE)
	public Response orderUpdate(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderLogAddRequest req = new UserOrderLogAddRequest();
			req.parse(request);
			response = userOrderService.updateOrderLog(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_ORDER_DELETE)
	public Response orderDelete(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderLogDelRequest req = new UserOrderLogDelRequest();
			req.parse(request);
			response = userOrderService.deleteOrderLog(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_ORDER_LIST)
	public Response orderList(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderListRequest req = new UserOrderListRequest();
			req.parse(request);
			response = userOrderService.getList(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_ORDER_DETAIL)
	public Response orderDetail(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderDetailRequest req = new UserOrderDetailRequest();
			req.parse(request);
			response = userOrderService.getById(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_ORDER_ADD)
	public Response orderAdd(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderAddRequest req = new UserOrderAddRequest();
			req.parse(request);
			response = userOrderService.insert(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 累积用户金额、订单数，同时评估会员等级
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_ORDER_ACCUMULATES)
    public Response orderAccumulates(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserOrderAccumulatesRequest userOrderAccumulatesRequest = new UserOrderAccumulatesRequest();
        	userOrderAccumulatesRequest.parse(request);
        	response = userOrderService.accumulates(userOrderAccumulatesRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 取消订单，回滚VIP
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_ORDER_BACK)
	public Response orderBack(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserOrderBackRequest userOrderBackRequest = new UserOrderBackRequest();
			userOrderBackRequest.parse(request);
			response = userOrderService.levelBack(userOrderBackRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
}
